package com.linkstec;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class GenHolidaySqlFile {

	public static String[] holidays = 
			// 元旦
		{ "20221231", "20230101", "20230102",
				// 春节
				"20230121", "20230122",	"20230123", "20230124", "20230125", "20230126", "20230127",
				// 清明节
				"20230405",
				// 劳动节
				"20230429", "20230430",	"20230501", "20230502", "20230503",
				// 端午节
				"20230622", "20230623", "20230624",
				// 中秋节
				"20230929", "20230930",
				// 国庆节
				"20231001", "20231002", "20231003",	"20231004", "20231005", "20231006" };
	public static void genMotHoliday() {
		
		try {
			String deleteSql = "DELETE FROM T99_ACALENDAR_HRS WHERE CALENDAR_DATE BETWEEN 20221231 AND 20231231;";
			String inserItemSql = "INSERT INTO \"T99_ACALENDAR_HRS\" (\"CALENDAR_DATE\", \"DATE_TYPE\", \"HOLD_COMP_ID\", \"BIZ_DATE\", \"UPDDATE_TIME\", \"SYS_TYPE\", "
					+ "\"REC_GEN_TIME\", \"REC_UPD_TIME\", \"DEL_FLG\", \"GEN_MODULE_ID\", \"GEN_USER_ID\", \"UPD_MODULE_ID\", \"UPD_USER_ID\") "
					+ "VALUES ('%s','%s', 1, '%s', NULL, 'HRS', NULL, NULL, 0, NULL, NULL, NULL, NULL);";
			
			DateFormat df = new SimpleDateFormat("yyyyMMdd");
			
			Calendar startDate = Calendar.getInstance();
			startDate.setTime(df.parse("20221231"));
			Calendar endDate = Calendar.getInstance();
			endDate.setTime(df.parse("20231231"));
			
			File file = new File("d:\\【MOT产品】-【2023日历】.txt");
			// 文件不存在时候，主动创建文件。
			if (!file.exists()) {
				file.createNewFile();
			}
			String bizDate = "20221230";
			FileWriter fw = new FileWriter(file, false);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(deleteSql + "\n");
			while (startDate.compareTo(endDate) <= 0) {
				int weekDay = startDate.get(Calendar.DAY_OF_WEEK);
				String currentDate = df.format(startDate.getTime());
				boolean holidayFlg = false;
				for (int i = 0; i < holidays.length; i++) {
					if (currentDate.equals(holidays[i])) {
						holidayFlg = true;
						break;
					}
				}
				String dateType = "0";
				if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) {
					
					dateType = "1";
				}
				if (holidayFlg) {
					dateType = "2";
				}
				if (dateType.equals("0")) {
					bizDate = currentDate;
				}
				bw.write(String.format(inserItemSql, currentDate, dateType,
						bizDate));
				bw.write("\n");
				startDate.add(Calendar.DAY_OF_MONTH, 1);
			}
			bw.write("COMMIT;\n");
			bw.close();
			fw.close();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 分布式任务调度节假日生成器
	 */
	public static void genFlowjobHoliday() {
		
		try {
			String deleteSql = "DELETE FROM SYS_CALENDAR WHERE DAY BETWEEN 20221231 AND 20231231;";
			String inserItemSql = "insert into SYS_CALENDAR (DAY, EXCHANGE_DAY, WEEK_DAY, WEEK, MONTH, QUARTER, YEAR, TRADE_FLAG, HOLIDAY_FLAG, MONTH_FIN) "
					+ " values ('%s', '%s', '%s', '%s', '%s', '%s', '2023', '%s', '%s', null);";
			
			DateFormat df = new SimpleDateFormat("yyyyMMdd");
			DateFormat monthdf = new SimpleDateFormat("yyyyMM");
			
			Calendar startDate = Calendar.getInstance();
			startDate.setTime(df.parse("20221231"));
			Calendar endDate = Calendar.getInstance();
			endDate.setTime(df.parse("20231231"));
			
			File file = new File("d:\\【MOT分布式调度】-【2023日历】.txt");
			// 文件不存在时候，主动创建文件。
			if (!file.exists()) {
				file.createNewFile();
			}
			String bizDate = "20221230";
			FileWriter fw = new FileWriter(file, false);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(deleteSql + "\n");
			Integer week = 202301;
			Integer quarter = 202300;
			while (startDate.compareTo(endDate) <= 0) {
				int weekDay = startDate.get(Calendar.DAY_OF_WEEK);
				String currentDate = df.format(startDate.getTime());
				Integer weekDayTemp = null;
				String month = monthdf.format(startDate.getTime());
				Integer tradeFlag = null;
				Integer holidayFlag = null;
				boolean holidayFlg = false;
				for (int i = 0; i < holidays.length; i++) {
					if (currentDate.equals(holidays[i])) {
						holidayFlg = true;
						break;
					}
				}
				String dateType = "0";
				if (weekDay == Calendar.SUNDAY) {
					weekDayTemp = 7;
				} else {
					weekDayTemp = weekDay -1;
				}
				
				if (weekDay == Calendar.MONDAY && !"20230101".equals(currentDate)) {
					week++;
				}
				
				if (Integer.parseInt(month) >= 202303) {
					quarter = 202301;
				}
				if (Integer.parseInt(month) >= 202306) {
					quarter = 202302;
				}
				if (Integer.parseInt(month) >= 202309) {
					quarter = 202303;
				}
				if (Integer.parseInt(month) >= 202312) {
					quarter = 202304;
				}
				if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) {
					
					dateType = "1";
					holidayFlag = 0;
					tradeFlag = 0;
				}
				if (holidayFlg) {
					dateType = "2";
					holidayFlag = 1;
					tradeFlag = 0;
				}
				if (dateType.equals("0")) {
					bizDate = currentDate;
					holidayFlag = 0;
					tradeFlag = 1;
				}
				bw.write(String.format(inserItemSql, currentDate, bizDate, weekDayTemp, week, month, quarter, tradeFlag, holidayFlag));
				bw.write("\n");
				startDate.add(Calendar.DAY_OF_MONTH, 1);
			}
			bw.write("COMMIT;\n");
			bw.close();
			fw.close();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		genMotHoliday();
		genFlowjobHoliday();

	}
}